﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Apply
{
    public class InvoiceAppService: ApplicationService, IInvoiceAppService
    {
        private readonly IRepository<invoice, int> _invoiceRepository;
        private readonly LogisticsDbContext _logisticsDbContext;

        public InvoiceAppService(IRepository<invoice, int> invoiceRepository, LogisticsDbContext logisticsDbContext)
        {
            _logisticsDbContext = logisticsDbContext;
            _invoiceRepository = invoiceRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Procurement"></param>
        /// <returns></returns>
        public async Task<InvoiceDto> CreateAsync(InvoiceDto Procurement)
        {
            var invoice = new invoice
            {
                ClerkName = Procurement.ClerkName,
                InvoiceNo = Procurement.InvoiceNo,
                InvoicePrice = Procurement.InvoicePrice,
                InvoiceType = Procurement.InvoiceType,
                InvoicingOffice=Procurement.InvoicingOffice,
                Preparer=Procurement.Preparer,
                Remarks=Procurement.Remarks,
                TaxAmount=Procurement.TaxAmount,
                taxRate=Procurement.taxRate,
                CreateTime=DateTime.Now,
                InvoiceTime=DateTime.Now,
            };

            await _invoiceRepository.InsertAsync(invoice);
            return ObjectMapper.Map<invoice, InvoiceDto>(invoice);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _invoiceRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<InvoiceDto> GetAsync(int id)
        {
            var invoice = await _invoiceRepository.GetAsync(id);
            return ObjectMapper.Map<invoice, InvoiceDto>(invoice);
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public async Task<PagedResultDto<InvoiceDto>> GetListAsync(GetInvoiceListDto dto)
        {
            var founder = _logisticsDbContext.Founder.ToList();
            var invoice = _logisticsDbContext.invoice.ToList();
            var joinInvo = from a in invoice
                           join c in founder on a.Preparer equals c.Id
                           select new invoice
                           {
                               Id = a.Id,
                               ClerkName = a.ClerkName,
                               InvoiceNo = a.InvoiceNo,
                               CreateTime=a.CreateTime,
                               InvoicePrice=a.InvoicePrice,
                               InvoiceType=a.InvoiceType,
                               InvoiceTime=a.InvoiceTime,
                               InvoicingOffice=a.InvoicingOffice,
                               Preparer=a.Preparer,
                               Remarks=a.Remarks,
                               TaxAmount=a.TaxAmount,
                               taxRate= a.taxRate,
                               Name=c.Name
                           };
            int totoCount = joinInvo.Count();
            var data = new PagedResultDto<InvoiceDto>();
            data.TotalCount = totoCount;
            joinInvo = joinInvo.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            data.Items = ObjectMapper.Map<List<invoice>, List<InvoiceDto>>(joinInvo.ToList());
            return data;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Gathering"></param>
        /// <returns></returns>

        public int UpdateAsync(InvoiceDto Invoice)
        {
            var invoice = new invoice
            {
                ClerkName= Invoice.ClerkName,
                CreateTime= DateTime.Now,
                InvoiceNo= Invoice.InvoiceNo,
                Id= Invoice.Id,
                InvoicePrice= Invoice.InvoicePrice,
                InvoiceTime= DateTime.Now,
                InvoiceType= Invoice.InvoiceType,
                InvoicingOffice= Invoice.InvoicingOffice,
                Preparer= Invoice.Preparer,
                Remarks= Invoice.Remarks,
                TaxAmount= Invoice.TaxAmount,
                taxRate= Invoice.taxRate,
            };
            _logisticsDbContext.Entry(invoice).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            var i = _logisticsDbContext.SaveChanges();
            return i;
        }
    }
}
